package com.seed.databasedesign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seed.databasedesign.entity.dict.DictDocumentEntity;
import com.seed.databasedesign.entity.dict.DictEntity;
import com.seed.databasedesign.entity.dict.DictItemEntity;
import com.seed.databasedesign.entity.table.ColumnEntity;
import com.seed.databasedesign.entity.table.TableDocumentEntity;
import com.seed.databasedesign.entity.table.TableEntity;
import com.seed.databasedesign.service.DataBaseDesignService;
import com.seed.databasedesign.type.DocumentTypeEnum;
import com.seed.util.EntityUtil;
import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jeecg.modules.online.cgform.entity.OnlCgformHead;
import org.jeecg.modules.online.cgform.mapper.OnlCgformFieldMapper;
import org.jeecg.modules.online.cgform.mapper.OnlCgformHeadMapper;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.ISysDictService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.seed.databasedesign.entity.dict.DictEnum.*;
import static com.seed.databasedesign.entity.table.TableColumnEnum.NAME;
import static com.seed.databasedesign.type.DocumentTypeEnum.MARKDOWN;

/**
 * @author gchiaway
 * 日期: 2020-05-24
 * 时间: 13:59
 */
@Slf4j
@Service("dataBaseDesignService")
public class DataBaseDesignServiceImpl implements DataBaseDesignService {

    @Resource
    private OnlCgformHeadMapper cgformHeadMapper;
    @Resource
    private OnlCgformFieldMapper cgformFieldMapper;
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private ISysDictItemService sysDictItemService;


    /**
     * 获取freemarker模板配置文件
     *
     * @param documentTypeEnum 生成策略
     * @return freemarker 模板配置文件
     * @throws IOException 可能会因为找不到文件而抛出文件错误
     */
    private Configuration getConfiguration(DocumentTypeEnum documentTypeEnum) throws IOException {
        URL resource = this.getClass().getClassLoader().getResource(documentTypeEnum.getPath());
        if (StringUtils.isEmpty(resource)) {
            return null;
        }
        Configuration cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(new File(resource.getFile()));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
        return cfg;
    }

    /**
     * 根据模板生成文件
     *
     * @param dirPath             生成目标路径
     * @param documentTypeEnum    生成策略
     * @param tableDocumentEntity 数据库文档
     * @param dictDocumentEntity  字段文档
     * @return 是否成功
     */
    private Boolean genFile(String dirPath,
                            DocumentTypeEnum documentTypeEnum,
                            TableDocumentEntity tableDocumentEntity,
                            DictDocumentEntity dictDocumentEntity) {
        Configuration configuration;
        try {
            configuration = this.getConfiguration(documentTypeEnum);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        if (StringUtils.isEmpty(configuration)) {
            log.error("获取Configuration出错,Configuration为空");
            return false;
        }
        String fileName = File.separator + tableDocumentEntity.getProjectName() + "数据库文档信息.md";
        File tempFile = new File(dirPath + fileName);
        if (!tempFile.getParentFile().exists()) {
            if (!tempFile.getParentFile().mkdirs()) {
                return false;
            }
        }
        try {
            configuration.getTemplate(documentTypeEnum.getDataBaseName())
                    .process(EntityUtil.beanToMap(tableDocumentEntity), new FileWriter(tempFile));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("数据库文档生成文件失败");
            return false;
        }

        String dictFileName = File.separator + dictDocumentEntity.getProjectName() + "字典文档信息.md";
        File dictTempFile = new File(dirPath + dictFileName);
        if (!dictTempFile.getParentFile().exists()) {
            if (!dictTempFile.getParentFile().mkdirs()) {
                return false;
            }
        }
        try {
            configuration.getTemplate(documentTypeEnum.getDictName())
                    .process(EntityUtil.beanToMap(dictDocumentEntity), new FileWriter(dictTempFile));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("字典文档生成文件失败");
            return false;
        }
        return true;
    }

    /**
     * 表对象转换
     *
     * @param projectName   项目名称
     * @param tableHeadList jeecg表对象
     * @return 文档生成表对象
     */
    private List<TableEntity> getTableEntityList(String projectName, List<OnlCgformHead> tableHeadList) {
        List<TableEntity> tableEntityList = new ArrayList<>();
        for (OnlCgformHead onlCgformHead : tableHeadList) {
            TableEntity tableEntity = new TableEntity();
            tableEntity.setContent(onlCgformHead.getTableTxt());
            tableEntity.setTableName(onlCgformHead.getTableName());
            tableEntity.setType(onlCgformHead.getTableName().replaceAll(projectName + "_", "").split("_")[0]);

            List<ColumnEntity> columnEntityList = new ArrayList<>();

            LambdaQueryWrapper<OnlCgformField> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OnlCgformField::getCgformHeadId, onlCgformHead.getId());
            List<OnlCgformField> fieldList = cgformFieldMapper.selectList(queryWrapper);

            for (OnlCgformField cgFormFieldEntity : fieldList) {
                ColumnEntity columnEntity = new ColumnEntity();
//                columnEntity.setFieldName(cgFormFieldEntity.getDbFieldName());
//                columnEntity.setType(cgFormFieldEntity.getDbType());
//                columnEntity.setFieldDefault(cgFormFieldEntity.getFieldDefaultValue());
//                columnEntity.setContent(cgFormFieldEntity.getDbFieldTxt());
//                columnEntity.setLength(cgFormFieldEntity.getDbLength());
//                columnEntity.setPointLength(cgFormFieldEntity.getDbPointLength());
//                columnEntity.setDictName(cgFormFieldEntity.getDictField());
//                if (cgFormFieldEntity.getDbIsKey() == 1) {
//                    columnEntity.setIsKey("是");
//                } else {
//                    columnEntity.setIsKey("否");
//                }
//                if (cgFormFieldEntity.getDbIsNull() == 1) {
//                    columnEntity.setIsNull("是");
//                } else {
//                    columnEntity.setIsNull("否");
//                }
                columnEntityList.add(columnEntity);
            }
            tableEntity.setColumnEntityList(columnEntityList);
            tableEntityList.add(tableEntity);
        }
        return tableEntityList;
    }


    /**
     * 表对象转换
     *
     * @param projectName 项目名称
     * @param dictList    jeecg表对象
     * @return 文档生成表对象
     */
    private List<DictEntity> getDictEntityList(String projectName, List<SysDict> dictList) {
        List<DictEntity> dictEntityList = new ArrayList<>();
        for (SysDict dict : dictList) {
            DictEntity dictEntity = new DictEntity();
            dictEntity.setName(dict.getDictName());
            dictEntity.setCode(dict.getDictCode());
            dictEntity.setType(dict.getDictCode().replaceAll(projectName + "_", "").split("_")[0]);

            List<DictItemEntity> dictItemEntityList = new ArrayList<>();

            LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysDictItem::getDictId, dict.getId());
            List<SysDictItem> dictItemList = sysDictItemService.list(queryWrapper);

            for (SysDictItem dictItem : dictItemList) {
                DictItemEntity dictItemEntity = new DictItemEntity();
                dictItemEntity.setDescription(dictItem.getDescription());
                dictItemEntity.setItemText(dictItem.getItemText());
                dictItemEntity.setItemValue(dictItem.getItemValue());
                dictItemEntity.setSortOrder(dictItem.getSortOrder());
                dictItemEntityList.add(dictItemEntity);
            }
            dictEntity.setDictItemEntityList(dictItemEntityList);
            dictEntityList.add(dictEntity);
        }
        return dictEntityList;
    }

    /**
     * 创建文档实体
     *
     * @param projectName 项目名称
     * @return 文档实体
     */
    private TableDocumentEntity getDocumentEntity(String projectName) {
        LambdaQueryWrapper<OnlCgformHead> queryWrapper = new LambdaQueryWrapper<>();
        //TODO:不能正确的查询譬如说,projectname是 see也会把seed的查询出来,暂时不知道怎么办
        queryWrapper.likeRight(OnlCgformHead::getTableName, projectName + "_");
        List<OnlCgformHead> tableHeadList = cgformHeadMapper.selectList(queryWrapper);
        if (StringUtils.isEmpty(tableHeadList) || tableHeadList.isEmpty()) {
            return null;
        }
        List<TableEntity> tableEntityList = this.getTableEntityList(projectName, tableHeadList);
        if (StringUtils.isEmpty(tableEntityList) || tableEntityList.isEmpty()) {
            return null;
        }
        TableDocumentEntity tableDocumentEntity = new TableDocumentEntity();
        tableDocumentEntity.setProjectName(projectName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        tableDocumentEntity.setExportDate(sdf.format(new Date()));

        Map<String, List<TableEntity>> tableEntityMap = tableEntityList.stream().collect(Collectors.groupingBy(TableEntity::getType));
        tableDocumentEntity.setTableEntityList(tableEntityMap);
        return tableDocumentEntity;
    }

    /**
     * 创建文档实体
     *
     * @param projectName 项目名称
     * @return 文档实体
     */
    private DictDocumentEntity getDictDocumentEntity(String projectName) {
        LambdaQueryWrapper<SysDict> queryWrapper = new LambdaQueryWrapper<>();
        //TODO:不能正确的查询譬如说,projectname是 see也会把seed的查询出来,暂时不知道怎么办
        queryWrapper.likeRight(SysDict::getDictCode, projectName + "_");
        List<SysDict> dictList = sysDictService.list(queryWrapper);
        if (StringUtils.isEmpty(dictList) || dictList.isEmpty()) {
            return null;
        }

        List<DictEntity> dictEntityList = this.getDictEntityList(projectName, dictList);
        if (StringUtils.isEmpty(dictEntityList) || dictEntityList.isEmpty()) {
            return null;
        }

        DictDocumentEntity dictDocumentEntity = new DictDocumentEntity();
        dictDocumentEntity.setProjectName(projectName);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        dictDocumentEntity.setExportDate(sdf.format(new Date()));

        Map<String, List<DictEntity>> dictEntityMap = dictEntityList.stream().collect(Collectors.groupingBy(DictEntity::getType));
        dictDocumentEntity.setDictEntityList(dictEntityMap);

        return dictDocumentEntity;
    }


    /**
     * 导出markdown格式的数据库文档
     *
     * @param outFilePath 导出路径
     * @param projectName 项目名称
     * @return 是否成功
     */
    @Override
    public Boolean exportDataBase(String outFilePath, String projectName) {
        TableDocumentEntity tableDocumentEntity = this.getDocumentEntity(projectName);
        if (StringUtils.isEmpty(tableDocumentEntity)) {
            return false;
        }
        DictDocumentEntity dictDocumentEntity = this.getDictDocumentEntity(projectName);
        if (StringUtils.isEmpty(dictDocumentEntity)) {
            return false;
        }
        return this.genFile(outFilePath, MARKDOWN, tableDocumentEntity, dictDocumentEntity);
    }

    /**
     * 用markdown文件转换为HTML文件
     *
     * @param markdownFilePath markdown文件路径
     * @return HTML文件
     */
    private Document getDocument(String markdownFilePath) {
        List<Extension> extensions = Arrays.asList(TablesExtension.create());
        Parser parser = Parser.builder()
                .extensions(extensions)
                .build();
        HtmlRenderer renderer = HtmlRenderer.builder()
                .extensions(extensions)
                .build();
        Node document;
        try (Reader reader = new FileReader(new File(markdownFilePath))) {
            document = parser.parseReader(reader);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        String html = renderer.render(document);
        return Jsoup.parseBodyFragment(html);
    }

    /**
     * 导入markdown格式的字典文档
     *
     * @param inFilePath  导入路径
     * @param importDicts 需要导入的字典
     * @return 是否成功
     */
    @Override
    public Boolean importDict(String inFilePath, String... importDicts) {
        List<String> dictArray = new ArrayList<>();
        if (!StringUtils.isEmpty(importDicts) && importDicts.length > 0) {
            dictArray = Arrays.asList(importDicts);
        }
        Document doc = this.getDocument(inFilePath);
        if (StringUtils.isEmpty(doc)) {
            return false;
        }
        Elements dictNames = doc.getElementsByTag("h4");
        Elements dicts = doc.getElementsByTag("table");
        for (int i = 0; i < dicts.size(); i++) {
            Element dictNameElement = dictNames.get(i);
            String dictNameAndName = dictNameElement.text().replaceAll(" ", "");
            String dictCode = dictNameAndName.split("[(]", 2)[0];
            String dictName = dictNameAndName.split("[(]", 2)[1].replace(")", "");
            if (!dictArray.isEmpty()) {
                if (!dictArray.contains(dictCode)) {
                    continue;
                }
            }
            LambdaQueryWrapper<SysDict> sysDictLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysDictLambdaQueryWrapper.eq(SysDict::getDictCode, dictCode);
            List<SysDict> dictList = sysDictService.list(sysDictLambdaQueryWrapper);
            SysDict dict;
            if (StringUtils.isEmpty(dictList) || dictList.isEmpty()) {
                dict = new SysDict();
                dict.setDelFlag(0);
                dict.setType(0);
                dict.setDictName(dictName);
                dict.setDictCode(dictCode);
                if (!sysDictService.save(dict)) {
                    return false;
                }
            } else {
                dict = dictList.get(0);
                if (1 == dict.getDelFlag()) {
                    dict.setDelFlag(0);
                    sysDictService.updateById(dict);
                }
                LambdaQueryWrapper<SysDictItem> deleteWrapper = new LambdaQueryWrapper<>();
                deleteWrapper.eq(SysDictItem::getDictId, dict.getId());
                sysDictItemService.remove(deleteWrapper);
            }
            Element dictElement = dicts.get(i);
            Elements trs = dictElement.select("tr");
            for (int j = 1; j < trs.size(); j++) {
                Element tr = trs.get(j);
                Elements tds = tr.select("td");
                SysDictItem dictItem = new SysDictItem();
                dictItem.setDictId(dict.getId());
                dictItem.setStatus(1);
                dictItem.setSortOrder(Integer.valueOf(tds.get(SORT_ORDER.getIndex()).text().replaceAll(" ", "")));
                dictItem.setItemText(tds.get(ITEM_TEXT.getIndex()).text().replaceAll(" ", ""));
                dictItem.setItemValue(tds.get(ITEM_VALUE.getIndex()).text().replaceAll(" ", ""));
                dictItem.setDescription(tds.get(DESCRIPTION.getIndex()).text().replaceAll(" ", ""));
                if (!sysDictItemService.save(dictItem)) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 导入markdown格式的数据库文档
     *
     * @param inFilePath   导入路径
     * @param importTables 需要导入的表
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean importDataBase(String inFilePath, String... importTables) {
        List<String> tablesArray = new ArrayList<>();
        if (!StringUtils.isEmpty(importTables) && importTables.length > 0) {
            tablesArray = Arrays.asList(importTables);
        }
        Document doc = this.getDocument(inFilePath);
        if (StringUtils.isEmpty(doc)) {
            return false;
        }
        Elements tableNames = doc.getElementsByTag("h4");
        Elements tables = doc.getElementsByTag("table");
        for (int i = 0; i < tables.size(); i++) {
            Element tableNameElement = tableNames.get(i);
            String tableNameAndTxt = tableNameElement.text().replaceAll(" ", "");
            String tableName = tableNameAndTxt.split("[(]", 2)[0];
            String tableTxt = tableNameAndTxt.split("[(]", 2)[1].replace(")", "");
            if (!tablesArray.isEmpty()) {
                if (!tablesArray.contains(tableName)) {
                    continue;
                }
            }
            LambdaQueryWrapper<OnlCgformHead> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OnlCgformHead::getTableName, tableName);
            List<OnlCgformHead> tableHeadList = cgformHeadMapper.selectList(queryWrapper);
            OnlCgformHead cgformHead;
            if (StringUtils.isEmpty(tableHeadList) || tableHeadList.isEmpty()) {
                cgformHead = new OnlCgformHead();
                cgformHead.setTableName(tableName);
                //崭新的表
                cgformHead.setTableTxt(tableTxt);
                cgformHead.setTableVersion(1);
                cgformHead.setTableType(1);
                cgformHead.setIsCheckbox("Y");
                cgformHead.setIsDbSynch("N");
                cgformHead.setIsPage("Y");
                cgformHead.setIsTree("N");
                cgformHead.setIdType("UUID");
                cgformHead.setQueryMode("single");
                cgformHead.setFormCategory("temp");
                cgformHead.setFormTemplate("1");
                cgformHead.setScroll(1);
                cgformHead.setCopyType(0);
                cgformHead.setThemeTemplate("normal");
                if (cgformHeadMapper.insert(cgformHead) == 0) {
                    return false;
                }
            } else {
                //老的表
                cgformHead = tableHeadList.get(0);
                cgformHead.setIsDbSynch("N");
                if (cgformHeadMapper.updateById(cgformHead) <= 0) {
                    return false;
                }
                //旧的字段全删除了,再录入新的字段
                LambdaQueryWrapper<OnlCgformField> deleteWrapper = new LambdaQueryWrapper<>();
                deleteWrapper.eq(OnlCgformField::getCgformHeadId, cgformHead.getId());
                cgformFieldMapper.delete(deleteWrapper);
            }
            Element tableElement = tables.get(i);
            Elements trs = tableElement.select("tr");
            if (!this.insertDefaultField(cgformHead.getId(), trs.size() - 1)) {
                return false;
            }
            List<String> defaultFields = Arrays.asList("id", "sys_org_code", "update_by", "create_time", "create_by", "update_time");
            for (int j = 1; j < trs.size(); j++) {
                Element tr = trs.get(j);
                Elements tds = tr.select("td");
                String fieldName = tds.get(NAME.getIndex()).text();
                if (defaultFields.contains(fieldName)) {
                    continue;
                }
                ColumnEntity columnEntity = new ColumnEntity(tds);
                OnlCgformField cgformField = columnEntity.toCgformField();
                cgformField.setCgformHeadId(cgformHead.getId());
                cgformField.setOrderNum(j + 1);
                if (cgformFieldMapper.insert(cgformField) == 0) {
                    return false;
                }
            }
        }
        return true;
    }


    /**
     * 插入默认数据
     *
     * @param cgformHeadId 在线表单id
     * @param count        新字段数量
     * @return 是否成功
     */
    private Boolean insertDefaultField(String cgformHeadId, int count) {
        OnlCgformField cgformField = new OnlCgformField();
        cgformField.setCgformHeadId(cgformHeadId);
        cgformField.setQueryConfigFlag("0");
        cgformField.setSortFlag("0");
        cgformField.setDbPointLength(0);
        cgformField.setDbDefaultVal("");
        cgformField.setQueryConfigFlag("0");
        cgformField.setSortFlag("0");
        cgformField.setQueryMode("single");
        cgformField.setIsQuery(0);
        cgformField.setIsShowForm(0);
        cgformField.setFieldLength(120);
        cgformField.setFieldMustInput("0");

        cgformField.setOrderNum(1);
        cgformField.setDbFieldName("id");
        cgformField.setDbFieldTxt("主键");
        cgformField.setDbIsKey(1);
        cgformField.setDbIsNull(0);
        cgformField.setIsShowList(0);
        cgformField.setIsReadOnly(1);
        cgformField.setDbType("string");
        cgformField.setFieldShowType("text");
        cgformField.setDbLength(36);
        cgformField.setId(null);
        if (cgformFieldMapper.insert(cgformField) == 0) {
            return false;
        }

        cgformField.setIsShowList(0);
        cgformField.setOrderNum(count + 2);
        cgformField.setDbFieldName("sys_org_code");
        cgformField.setDbFieldTxt("所属部门");
        cgformField.setDbIsKey(0);
        cgformField.setDbIsNull(1);
        cgformField.setIsReadOnly(0);
        cgformField.setDbType("string");
        cgformField.setFieldShowType("text");
        cgformField.setDbLength(64);
        cgformField.setId(null);
        if (cgformFieldMapper.insert(cgformField) == 0) {
            return false;
        }

        cgformField.setIsShowList(1);
        cgformField.setOrderNum(count + 3);
        cgformField.setDbFieldName("create_time");
        cgformField.setDbFieldTxt("创建日期");
        cgformField.setDbIsKey(0);
        cgformField.setDbIsNull(1);
        cgformField.setIsReadOnly(0);
        cgformField.setDbType("Datetime");
        cgformField.setFieldShowType("datetime");
        cgformField.setDbLength(20);
        cgformField.setIsQuery(1);
        cgformField.setQueryMode("group");
        cgformField.setId(null);
        if (cgformFieldMapper.insert(cgformField) == 0) {
            return false;
        }

        cgformField.setIsShowList(0);
        cgformField.setIsQuery(0);
        cgformField.setQueryMode("single");

        cgformField.setOrderNum(count + 4);
        cgformField.setDbFieldName("create_by");
        cgformField.setDbFieldTxt("创建人");
        cgformField.setDbIsKey(0);
        cgformField.setDbIsNull(1);
        cgformField.setIsReadOnly(0);
        cgformField.setDbType("string");
        cgformField.setFieldShowType("text");
        cgformField.setDbLength(50);
        cgformField.setId(null);
        if (cgformFieldMapper.insert(cgformField) == 0) {
            return false;
        }

        cgformField.setOrderNum(count + 5);
        cgformField.setDbFieldName("create_user_id");
        cgformField.setDbFieldTxt("创建人id");
        cgformField.setDbIsKey(0);
        cgformField.setDbIsNull(1);
        cgformField.setIsReadOnly(0);
        cgformField.setDbType("string");
        cgformField.setFieldShowType("text");
        cgformField.setDbLength(50);
        cgformField.setId(null);
        if (cgformFieldMapper.insert(cgformField) == 0) {
            return false;
        }

        cgformField.setOrderNum(count + 6);
        cgformField.setDbFieldName("update_time");
        cgformField.setDbFieldTxt("更新日期");
        cgformField.setDbIsKey(0);
        cgformField.setDbIsNull(1);
        cgformField.setIsReadOnly(0);
        cgformField.setDbType("Datetime");
        cgformField.setFieldShowType("datetime");
        cgformField.setDbLength(20);
        cgformField.setId(null);
        if (cgformFieldMapper.insert(cgformField) == 0) {
            return false;
        }

        cgformField.setOrderNum(count + 7);
        cgformField.setDbFieldName("update_by");
        cgformField.setDbFieldTxt("更新人");
        cgformField.setDbIsKey(0);
        cgformField.setDbIsNull(1);
        cgformField.setIsReadOnly(0);
        cgformField.setDbType("string");
        cgformField.setFieldShowType("text");
        cgformField.setDbLength(50);
        cgformField.setId(null);
        if (cgformFieldMapper.insert(cgformField) == 0) {
            return false;
        }

        cgformField.setOrderNum(count + 8);
        cgformField.setDbFieldName("update_user_id");
        cgformField.setDbFieldTxt("更新人id");
        cgformField.setDbIsKey(0);
        cgformField.setDbIsNull(1);
        cgformField.setIsReadOnly(0);
        cgformField.setDbType("string");
        cgformField.setFieldShowType("text");
        cgformField.setDbLength(50);
        cgformField.setId(null);
        if (cgformFieldMapper.insert(cgformField) == 0) {
            return false;
        }

        return true;
    }
}
